package com.nbpt.edu.commercesites.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nbpt.edu.commercesites.mapper.GoodMapper;
import com.nbpt.edu.commercesites.mapper.PositionMapper;
import com.nbpt.edu.commercesites.mapper.StaffMapper;
import com.nbpt.edu.commercesites.service.StaffService;
import com.nbpt.edu.commercesites.utils.BeanUtils;
import com.nbpt.edu.commercesites.utils.PasswordUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pojo.Good;
import pojo.Position;
import pojo.Staff;
import pojo.Type;
import pojo.entity.StaffEntity;
import pojo.vo.StaffVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {
    @Autowired
    StaffMapper staffMapper;
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    GoodMapper goodMapper;
    @Transactional
    @Override
    public Boolean login(String uName, String uPassword) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("u_username",uName);
        Staff staff = staffMapper.selectOne(queryWrapper);
        if(staff !=null && PasswordUtils.matches(uPassword,staff.getuPassword())){
            return true;
        }
        return false;
    }
    @Transactional
    @Override
    public Boolean register(Staff user) {
        Staff staff=new Staff();
        staff.setuUsername(user.getuUsername());
        BeanUtils.copyPropertiesIgnoreNull(user,staff);
        staff.setuPassword(PasswordUtils.encode(user.getuPassword()));
        staff.setuDelflag(false);
        staff.setuPositionId(4L);
        staff.setuCreateTime(user.getuCreateTime());
        staff.setuUpdateTime(user.getuUpdateTime());
        staffMapper.insert(staff);
        return true;
    }
    @Transactional
    @Override
    public Position getPosition(String u_username) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("u_username",u_username);
        Staff staff = staffMapper.selectOne(queryWrapper);
        Position position = positionMapper.selectById(staff.getuPositionId());

        return position;
    }
    @Transactional
    @Override
    public Staff getUser(String u_username) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("u_username",u_username);
        Staff staff = staffMapper.selectOne(queryWrapper);
        return staff;
    }
    @Transactional
    @Override
    public Boolean resetPassword(String u_username, String u_password) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("u_username",u_username);
        Staff staff = staffMapper.selectOne(queryWrapper);
        if(staff.getuId()!=null){
            staff.setuPassword(PasswordUtils.encode(u_password));
            staffMapper.DeleteById(staff.getuId());
            staffMapper.insert(staff);
            return true;
        }

        return false;
    }

    @Override
    public IPage<StaffVo> getCustomer(Integer current, Integer size) {
        Page<StaffVo> page = new Page<>(current, size);
       return staffMapper.getCustomerWithPosition(page);
    }
    @Transactional
    @Override
    public Boolean insetUser(StaffEntity staffEntity) {
        Staff staff=new Staff();
        BeanUtils.copyPropertiesIgnoreNull(staffEntity,staff);
        staff.setuPassword(PasswordUtils.encode(staffEntity.getuPassword()));
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("p_name",staffEntity.getPositionName());
        Position position = positionMapper.selectOne(queryWrapper);
        staff.setuPositionId(position.getpId());
        staffMapper.insert(staff);
        return true;
    }
    @Transactional
    @Override
    public Boolean deleteById(List<Long> ids) {
        return (staffMapper.deleteByIds(ids)>0);
    }
    @Transactional
    @Override
    public List<Long> getNumber() {
        long count = goodMapper.selectList(null).stream().count();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.ne("u_position_id",4L);
        long count1 = staffMapper.selectList(queryWrapper).stream().count();
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.ge("u_position_id",4L);
        long count2 = staffMapper.selectList(queryWrapper1).stream().count();
        List<Long> longs=new ArrayList<>();
        longs.add(count);
        longs.add(count1);
        longs.add(count2);

        return longs;
    }
    @Transactional
    @Override
    public Boolean updateUser(StaffVo staffVo) {
        if (staffVo.getuId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        // 查询现有数据
        Staff staff = staffMapper.selectById(staffVo.getuId());
        if (staff == null) {
            throw new RuntimeException("用户不存在");
        }

        // 复制前端传来的非空字段到已有实体对象中
        BeanUtils.copyPropertiesIgnoreNull(staff, staffVo);

        // 查询职位信息并设置职位ID
        if (staffVo.getPositionName() != null && !staffVo.getPositionName().isEmpty()) {
            LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<>();
            positionWrapper.eq(Position::getpName, staffVo.getPositionName());

            Position position = positionMapper.selectOne(positionWrapper);
            if (position == null) {
                throw new RuntimeException("职位不存在");
            }
            staff.setuPositionId(position.getpId());
        }

        // 更新时间戳
        staff.setuUpdateTime(new Date());

        // 使用 updateById 替代 update(staff, queryWrapper)
        return staffMapper.updateById(staff) > 0;
    }
    @Transactional
    @Override
    public List<String> getAllPosition() {
        List<String> list=new ArrayList<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.ne("p_id",4L);
        List<Position> positions = positionMapper.selectList(queryWrapper);
        for (Position position:positions
             ) {
            list.add(position.getpName());
        }
        return list;
    }


}
